package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {
      @Autowired
      private EmployeeService employeeService;
      @Autowired
      private JwtProperties jwtProperties;
  
      /**
       * 登录
       *
       * @param employeeLoginDTO
       * @return
       */
      @PostMapping("/login")
      @ApiOperation(value = "员工登录")
      public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
          log.info("员工登录：{}", employeeLoginDTO);
  
          Employee employee = employeeService.login(employeeLoginDTO);
  
          //登录成功后，生成jwt令牌
          Map<String, Object> claims = new HashMap<>();
          claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
          String token = JwtUtil.createJWT(
                  jwtProperties.getAdminSecretKey(),
                  jwtProperties.getAdminTtl(),
                  claims);
  
          EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                  .id(employee.getId())
                  .userName(employee.getUsername())
                  .name(employee.getName())
                  .token(token)
                  .build();
  
          return Result.success(employeeLoginVO);
      }
  
      /**
       * 退出
       *
       * @return
       */
      @PostMapping("/logout")
      public Result<String> logout() {
          return Result.success();
      }
    
    
    /**
     * 添加员工
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result add(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        employeeService.inster(employeeDTO);
        return Result.success();
      }
  
  
    /**
     * 分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO queryDTO) {
      log.info("分页查询参数：{}", queryDTO);
      PageResult page = employeeService.page(queryDTO);
      return Result.success(page);
    }
  
  /**
   * 启用禁用员工账号
   * code: 1为启用 0为禁用
   */
  @PostMapping("/status/{code}")
  public Result<?> changeStatus(@PathVariable Integer code, @RequestParam Long id) {
    log.info("启用禁用员工账号： {}, {}", id, code);
    employeeService.changeStatus(id, code);
    return Result.success();
  }
  
  /**
   * 修改员工信息。
   *
   * @param employeeDTO 包含员工更新信息的数据传输对象，包括但不限于员工ID、用户名、姓名等
   */
  @PutMapping
  public Result update(@RequestBody EmployeeDTO employeeDTO) {
    log.info("修改员工：{}", employeeDTO);
    employeeService.update(employeeDTO);
    return Result.success();
  }
  
  /**
   * 根据id查询员工信息
   */
  @GetMapping("/{id}")
  public Result<Employee> findById(@PathVariable Long id) {
    Employee employee = employeeService.findById(id);
    return Result.success(employee);
  }
  
  /**
   * 修改密码
   */
  @PutMapping("/editPassword")
  public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
    log.info("修改密码：{}", passwordEditDTO);
    employeeService.editPwd(passwordEditDTO);
    return Result.success();
  }
    

}
